package com.dsteiner.xelavos.client.domain;

import java.util.Date;

import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.dsteiner.widgets.client.ObjectUtil;
import com.dsteiner.widgets.client.domain.ModelObject;
import com.dsteiner.widgets.client.domain.Searchable;
import com.dsteiner.widgets.client.domain.SearchableId;
import com.dsteiner.widgets.client.domain.SearchableProperty;
import com.dsteiner.widgets.client.domain.SearchableRefId;
import com.dsteiner.xelavos.client.constants.Constants;
import com.google.appengine.api.datastore.Text;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
@Searchable
public class Service extends ModelObject implements HasImage, Rateable, Commentable, HasUser{

	@PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    @Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true")
    @SearchableId 
    private String id;

	@Persistent
    @SearchableRefId
    private String userId;
	@Persistent
    @SearchableProperty
    private String name;
	
	@Persistent
    @SearchableProperty
    private String type;
    
    @Persistent
    @SearchableProperty
    private String place;
    
    @Persistent(defaultFetchGroup="true")
    private Text description;
    
    @Persistent
    private Date registeredDate;
    
    @Persistent
    private int ratingCount;

    @Persistent
    @SearchableRefId
	private String imageId;
    
    @Persistent
    private String contact;

    @Persistent
	private int ratingTotal;

    @Persistent
    private int rating;

    @Persistent
	private int commentCount;
    
    @Persistent
	private String userNickName;

    public Service() {
	}

    public void addCommentCount(int count) {
		commentCount += count;
	}

    public void addRating(int rating) {
		ratingCount++;
		ratingTotal += rating;
		this.rating = Constants.Util.calculateRating(ratingTotal, ratingCount);
	}
    
    public boolean equals(Object obj) {
		if (obj == null)
			return false;
		if (!(obj instanceof Service))
			return false;
		Service other = (Service) obj;
		return ObjectUtil.equals(getId(), other.getId());
	}
    
    public int getCommentCount() {
		return commentCount;
	}
    
    // Accessors for the fields.  JDO doesn't use these, but your application does.

    
    
	public String getContact() {
		return contact;
	}
	
	public String getId(){
		return id;
	}
	public String getImageId() {
		return imageId;
	}

	public String getName() {
		return name;
	}

	public String getPlace() {
		return place;
	}

	public int getRating() {
    	return rating;
    }

	public int getRatingCount() {
		return ratingCount;
	}

	public int getRatingTotal() {
		return ratingTotal;
	}

	public Date getRegisteredDate() {
		return registeredDate;
	}

	public String getType() {
		return type;
	}
	public String getUserFirstName() {
		return userNickName;
	}
	public String getUserId() {
		return userId;
	}
	public void removeRating(int rating) {
		ratingCount--;
		ratingTotal -= rating;
		this.rating = Constants.Util.calculateRating(ratingTotal, ratingCount);
	}
	public void setCommentCount(int commentCount) {
		this.commentCount = commentCount;
	}
	public void setContact(String contact) {
		this.contact = contact;
	}
	public Text getDescription() {
		return description;
	}
	public void setDescription(Text description) {
		this.description = description;
	}
    public void setId(String id) {
		this.id = id;
	}
    public void setImageId(String imageId) {
		this.imageId = imageId;
	}
    public void setName(String name) {
		this.name = name;
	}
	public void setPlace(String place) {
		this.place = place;
	}
	public void setRating(int rating) {
		this.rating = rating;
	}
	public void setRatingCount(int ratingCount) {
		this.ratingCount = ratingCount;
	}
	public void setRatingTotal(int rating) {
		this.ratingTotal = rating;
	}
	public void setRegisteredDate(Date registeredDate) {
		this.registeredDate = registeredDate;
	}
	public void setType(String type) {
		this.type = type;
	}
	public void setUserFirstName(String userNickName) {
		this.userNickName = userNickName;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	}
	@Override
	public String toString() {
		return name;
	}
}